home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / HyperCard Related / XCMDs & XFCNs / Help XFCN 1.4 / source / gfun.c next >
Encoding:
C/C++ Source or Header  |  1993-06-13  |  3.9 KB  |  174 lines  |  [TEXT/MPS ]

  1. /*
  2.     gfun.c
  3.     
  4.     Functions to support storing data references
  5.     in a HyperCard global.  Allows data to be
  6.     kept between XFCN/XCMD calls.
  7.     
  8.     11-29-90 1.0d3 JRP    extract from XcmdFuncHC.c
  9.     
  10.     There are callbacks in this file:
  11.     
  12.         SetGlobal
  13.         GetGlobal
  14. */
  15.  
  16. #include    <Memory.h>
  17. #include    <HyperXCmd.h>
  18. #include    <String.h>
  19.  
  20. #include    "gfun.pro"
  21.  
  22. void convertHanToHexStr(hexStr, han)
  23. /*
  24.     Convert the Handle to 8-char (+ terminator) hex string representation.
  25. */
  26.     char    *hexStr;
  27.     Handle    han;
  28. {
  29.     short    digit,
  30.             shiftCnt = 32;
  31.     
  32.     do
  33.     {
  34.         shiftCnt -= 4;
  35.         digit = '0' + (((short) ( (long) han >> shiftCnt)) & 0x000F);
  36.         if(digit > '9')
  37.             digit += 7;                        /*    A-F hex digit    */
  38.         *hexStr++ = digit;
  39.     }
  40.     while(shiftCnt>0);
  41.     *hexStr = '\0';                            /*    add terminator    */
  42. }    /*    --------------------------------------------    convertHanToHexStr    */
  43.  
  44. void convertHexStrtoHan(han, hexStr)
  45. /*
  46.     Convert the hex string representation to a Handle.
  47.     The HexStr representation must be a C-string.
  48.     Only the least-significant 8 hex digits are used.
  49.     Assumes that handle is already locked.
  50.     
  51.     Returns nil for handle if there are not 8 hex digits.
  52. */
  53.     Handle    *han;
  54.     char    *hexStr;
  55. {
  56.     unsigned long    digit,
  57.                     hanVal=0L;
  58.     short            shiftCnt=0,
  59.                     i;
  60.     Str255            tempPstr;
  61.     
  62.     if(strlen(hexStr)!=8)
  63.     {
  64.         *han = nil;
  65.         return;
  66.     }
  67.     copyCtoPstr(tempPstr, hexStr);
  68.     for(i=tempPstr[0]; i>0 && shiftCnt < 32; i--)
  69.     {
  70.         digit = tempPstr[i] - '0';        /*    unsigned, result is positive    */
  71.         if(digit>9L)
  72.             digit -= 7L;                /*    A-F hex digit                    */
  73.         if(digit>'F')
  74.         {
  75.             *han = nil;
  76.             return;
  77.         }
  78.         digit <<= shiftCnt;                /*    shift left                        */
  79.         hanVal = hanVal | digit;        /*    insert value                    */
  80.         shiftCnt += 4;
  81.     }
  82.     *han = (Handle) hanVal;
  83. }    /*    --------------------------------------------    convertHexStrtoHan    */
  84.  
  85. void copyCtoPstr(Pstr, Cstr)
  86. /*
  87.     Copy the Cstr to the Pstr.
  88.     Allows us to make a P-string copy without
  89.     doing it in-place.
  90. */
  91.     Str255    Pstr;
  92.     char    *Cstr;
  93. {
  94.     short    i=0;
  95.     
  96.     while(*Cstr!=0)
  97.         Pstr[++i] = *Cstr++;
  98.     Pstr[0] = i;
  99. }    /*    --------------------------------------------    copyCtoPstr            */
  100.  
  101. /*
  102.     Allocate heapspace and copy a C string into it.
  103. */
  104. Handle copyStrToHand(str)
  105.     char *str;
  106. {
  107.     Handle    newHndl;
  108.  
  109.     newHndl = (Handle) NewHandle((long) strlen(str) + 1);
  110.     HLock(newHndl);
  111.     strcpy((char *)(*newHndl), str);
  112.     HUnlock(newHndl);
  113.     return(newHndl);
  114. }
  115.  
  116. short fetchHanFromGlobal(paramPtr, globalNamePstr, han)
  117. /*
  118.     Fetch the handle stored in HexStr
  119.     in the HC global container globalName.
  120.     
  121.     11-29-90 1.0d3 JRP    change globalName from C-string to P-string
  122.                         add test for empty globalName
  123.     
  124.     Returns result code:
  125.         TRUE        successful, found handle
  126.         FALSE        handle is nil
  127. */
  128.     XCmdPtr     paramPtr;                        /*    HyperCard connection    */
  129.     Str31        globalNamePstr;                    /*    name of global            */
  130.     Handle        *han;                            /*    pointer to handle        */
  131. {
  132.     Handle        hGlobalString;                    /*    string stored in global    */
  133.     
  134.     if(globalNamePstr[0]==0)
  135.         *han = nil;
  136.     else
  137.     {
  138.         HLock(hGlobalString = GetGlobal(paramPtr, globalNamePstr));
  139.         convertHexStrtoHan(han, *hGlobalString);
  140.         DisposHandle(hGlobalString);
  141.     }
  142.     return (*han!=nil);
  143. }    /*    --------------------------------------------    fetchHanFromGlobal    */
  144.  
  145. short storeHanInGlobal(paramPtr, globalNamePstr, han)
  146. /*
  147.     Convert the handle to HexStr representation and
  148.     store in the HC global container globalName.
  149.     If handle is zero, store empty in the global.
  150.     This lets the HyperCard script test to see if
  151.     memory has been allocated (not empty == allocated).
  152.     
  153.     11-29-90 1.0d3 JRP    change globalName from C-string to P-string
  154.  
  155.     Returns result code.
  156. */
  157.     XCmdPtr     paramPtr;
  158.     Str31        globalNamePstr;
  159.     Handle        han;
  160. {
  161.     Handle        tempHan;
  162.     char        hanCstr[16];
  163.     
  164.     if(han==nil)
  165.         *hanCstr = '\0';
  166.     else
  167.         convertHanToHexStr(hanCstr, han);    /*    convert handle to hex strng    */
  168.     tempHan = copyStrToHand(hanCstr);        /*    store string for passing    */
  169.     SetGlobal(paramPtr, globalNamePstr, tempHan);    /*    store contents        */
  170.     DisposHandle(tempHan);
  171.     return MemError();
  172. }    /*    --------------------------------------------    storeHanInGlobal    */
  173.  
  174.